Convert Strings to Numbers
- Line 2 converts the string represented by the currentAge to a number
var currentAge = prompt("Enter your age.");
var qualifyingAge = parseInt(currentAge) + 1;
- MyInteger will not be rounded. Insead of 2, 1 will be stored in myInteger
var myInteger = parseInt("1.9999");
- to preserve any decimal values, use parseFloat. In the following example, myFractional is assigned 1.9999
var myFractional = parseFloat("1.9999");